from django.shortcuts import render, HttpResponse
import random
# from utils.tencent.sms import send_sms_single
from django.conf import settings
from django import forms
from app01 import models
from unisdk.sms import UniSMS
from unisdk.exception import UniException
from django.core.validators import RegexValidator  # 手机号正则
from django.core.exceptions import ValidationError

# Create your views here.


def send_sms(phone_num, template_id, template_param_list):
    # 初始化
    client = UniSMS("your access key id", "your access key secret")  # 若使用简易验签模式仅传入第一个参数即可

    try:
        # 发送短信
        res = client.send({
            "to": "your phone number",
            "signature": "UniSMS",
            "templateId": "login_tmpl",
            "templateData": {
                "code": 7777
            }
        })
        print(res.data)
    except UniException as e:
        print(e)


class RegisterModelForm(forms.ModelForm):
    mobile_phone = forms.CharField(
        label='手机号',
        validators=[RegexValidator(r'^(1[3|4|5|6|7|8|9])\d{9}$', '手机号格式错误！')])
    password = forms.CharField(
        label="密码",
        widget=forms.PasswordInput())

    confirm_password = forms.CharField(
        label="重复密码",
        widget=forms.PasswordInput())
    code = forms.CharField(
        label="验证码",
        widget=forms.TextInput())

    class Meta:
        model = models.UserInfo
        fields = ["username", "email", "password", "confirm_password", "mobile_phone", "code"]

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for name, field in self.fields.items():
            field.widget.attrs['class'] = 'form-control'
            field.widget.attrs['placeholder'] = '请输入%s' % (field.label,)


def register(request):
    form = RegisterModelForm()
    return render(request, 'app01/register.html', {'form': form})



















